<?php

import("data.db.content.DbContentUnit");
import("data.logic.content.Article");
import("data.db.content.IContentBlock");
import("data.db.DbFactory");
import("data.logic.lingvo.Bundle");

class DbArticle extends DbContentUnit implements IContentBlock {
	
	public $className = "data.logic.content.Article";
	const CLASS_NAME = "data.logic.content.Article";
	//
	// SQL Table description section
	//
	const TB_TABLENAME	= "pd_textblock";
	const TB_I			= 'tbl';
	
	const TB_DATE		= 'tbl_date';
	const TB_AUTHOR		= 'tbl_author';
	const TB_TITLE		= 'tbl_title';
	const TB_SHORT		= 'tbl_short';
	const TB_FULL		= 'tbl_full';
	const TB_CATEGORY	= 'tbl_category';
	const TB_LOCALE		= 'tbl_locale';
	
	public static function _get_TABLENAME() {return self::TB_TABLENAME;}
	public static function _get_ID() {return self::TB_I."_id";}
	public static function _get_SUFFIX() {return self::TB_I;}
	//
	//
	
	// properties
	public $short;
	public $full;
	
	function __construct($noinit=false) {
		DbAbstractTable::__construct($noinit);
		$this->setDate(date("Y-m-d H:i:s"));
	}
	
	function getShortText() {
		return $this->short;
	}
	
	function setShortText($value) {
		$this->short = $value;
	}
	
	function getFullText() {
		return $this->full;
	}
	
	function setFullText($value) {
		$this->full = $value;
	}
	
	function getCategoryName() {
		$lingvo = Bundle::findByKey($this->getCategoryId());
		if ($lingvo) return $lingvo->getValue();
		return "";
	}
	
	// restore
	function fillEntity($arrResult) {
		DbAbstractTable::fillEntity($arrResult);
		$this->setDate($arrResult[self::TB_DATE]);
		$this->setAuthor($arrResult[self::TB_AUTHOR]);
		$this->setTitle($arrResult[self::TB_TITLE]);
		$this->setCategoryId($arrResult[self::TB_CATEGORY]);
		$this->setShortText($arrResult[self::TB_SHORT]);
		$this->setFullText($arrResult[self::TB_FULL]);
		$this->setLocale(DbFactory::getLocale($arrResult[self::TB_LOCALE]));
	}
	
	//store
	function fillFields() {
		DbAbstractTable::fillFields();
		$this->fildsForStore[self::TB_DATE] = "'".$this->getDate()."'";
		$this->fildsForStore[self::TB_AUTHOR] = "'".$this->getAuthor()."'";
		$this->fildsForStore[self::TB_TITLE] = "'".$this->getTitle()."'";
		$this->fildsForStore[self::TB_CATEGORY] = "'".$this->getCategoryId()."'";
		$this->fildsForStore[self::TB_SHORT] = "'".$this->getShortText()."'";
		$this->fildsForStore[self::TB_FULL] = "'".$this->getFullText()."'";
		$this->fildsForStore[self::TB_LOCALE] = "'".$this->locale->getId()."'";
	}
	
	function findAllByCategory($category, $local=null) {
		//TODO: category name must be replaced by category id
		return parent::__selectAllByCriteria(
			self::TB_CATEGORY."='".$category."'", self::CLASS_NAME, $local);
	}
	
	// same for all classes
	public static function findAll($local=null) {return DbAbstractTable::findAll(self::CLASS_NAME, $local);}
    public static function findById($id, $local=null) {return DbAbstractTable::findById($id, self::CLASS_NAME, $local);}
}
?>